<?php

class Ext_Oauth_Login
{
    protected $_storage = null;
    
    private $_adapter = null;
    private $_oauthResult = null;
    
    public function __construct()
    {
        $oauthResult = $this->getStorage()->setMember('oauthResult')->read();
        $this->_oauthResult = unserialize($oauthResult);
    }
    
    public function setAdapter($adapter)
    {
        $this->_adapter = $adapter;
    }
    
    public function setBind($userId)
    {
        if(!isset($this->_oauthResult)){
            return "";
        }
        if(!($this->_oauthResult instanceof Ext_Oauth_Login_Result)){
            return "";
        }
        
        $this->getStorage()->setMember('oauthResult')->clear();
        $userAccountOAuthRow = App_Model_DbTable_User_Account_OAuth::getInstance()->getRowByIdentitySite($this->_oauthResult->getDataIdentity(), $this->_oauthResult->getDataSite());
        if(!isset($userAccountOAuthRow['userId']) || $userAccountOAuthRow['userId'] < 1){
            if(isset($userAccountOAuthRow['id'])){
                $row = array(
                    'userId'      => $userId,
                );
                App_Model_DbTable_User_Account_OAuth::getInstance()->addOld($row, $_userAccountOAuthRow['id']);
            }else {
                $row = array(
                    'identity'     => $this->_oauthResult->getDataIdentity(),
                    'site'         => $this->_oauthResult->getDataSite(),
                    'userId'       => $userId,
                );
                App_Model_DbTable_User_Account_OAuth::getInstance()->addNew($row);
            }
        }
    }
    
    public function hasBind()
    {
        if(!isset($this->_oauthResult))
        {
            return false;
        }

        $userAccountOAuthRow = App_Model_DbTable_User_Account_OAuth::getInstance()->getRowByIdentitySite($this->_oauthResult->getDataIdentity(), $this->_oauthResult->getDataSite());
        if(isset($userAccountOAuthRow['userId'])){
            Ext_Auth_Front::getInstance()->auth($userAccountOAuthRow['userId']);
            $this->getStorage()->clear('oauthResult');
            
            return true;
        }
        
        return false;
    }

    public function hasOauth()
    {
        if(!isset($this->_oauthResult)){
            return false;
        }
        if(!($this->_oauthResult instanceof Ext_Oauth_Login_Result)){
            return false;
        }
        if($this->_oauthResult->getDataSite() != $this->_adapter){
            return false;
        }
        
        return true;
    }
    
    public function setOauth()
    {
        $oauthAdapter = new Ext_Oauth_Login_Adapter($this->_adapter);
        $oauthResult = $oauthAdapter->oauth();
        $this->getStorage()->setMember('oauthResult')->write(serialize($oauthResult));
        
        $this->_oauthResult = $oauthResult;
                
        return $this;
    }
    
    public function getOauthResult()
    {
        return $this->_oauthResult;
    }
    
    public function getStorage()
    {
        if (null === $this->_storage) {
            require_once 'Ext/Session.php';
            $this->setStorage(new Ext_Session());
        }

        return $this->_storage;
    }

    public function setStorage(Ext_Session $storage)
    {
        $this->_storage = $storage;
        return $this;
    }
}
